package org.daochong.lang.uai;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.daochong.lang.BeanUtil;
import org.daochong.lang.ucm.Configuration;

@SuppressWarnings("all")
public abstract class SimpleTemplateFactory<T> implements TemplateFactory<T> {

	private boolean singleton;
	private boolean threadLocal;
	private UniteAccessInterfaceFactory uaiFactory;

	public UniteAccessInterfaceFactory getUaiFactory() {
		return uaiFactory;
	}

	public void setUaiFactory(UniteAccessInterfaceFactory uaiFactory) {
		this.uaiFactory = uaiFactory;
	}

	public void init() {
		if (this.getUaiFactory() != null && this.getUaiFactory() instanceof DefaultUniteAccessInterfaceFactory) {
			((DefaultUniteAccessInterfaceFactory) this.getUaiFactory()).addTemplateFactory(this);
		}
	}

	public Class<?> getInfoClass() {
		return null;
	}

	private Class<T> clazz = null;

	public SimpleTemplateFactory() {
		Type genType = this.getClass().getGenericSuperclass();
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
		this.clazz = (Class<T>) params[0];
	}

	public Class<T> getBeanClass() {
		return clazz;
	}

	public boolean isSingleton() {
		return singleton;
	}

	public void setSingleton(boolean singleton) {
		this.singleton = singleton;
	}

	public boolean isThreadLocal() {
		return threadLocal;
	}

	public void setThreadLocal(boolean threadLocal) {
		this.threadLocal = threadLocal;
	}

	public void destory(T obj) {

	}

	protected void fill(Configuration config, T obj) {
		Map<String, String> map = config.getProperties().searchSubKey("template.");
		if (map != null && map.size() > 0) {
			Map<String, Object> params = new HashMap<String, Object>();
			for (String key : map.keySet()) {
				params.put(key, map.get(key));
			}
			BeanUtil.pushBean(params, obj);
		}
	}

}
